﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Task.Common.Interface;
using Common.Service;
using System.Collections;
using Task.Common.Entity;
using System.Data;
using System.Web.UI.HtmlControls;

namespace Task
{
    public partial class MyRead : System.Web.UI.Page
    {
        //ITask task = ServiceContainer.GetService<ITask>();
        IInboxRead read=ServiceContainer.GetService<IInboxRead>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Index = "0";
                LoadPageData(0);
                LoadFinPageData(0);
            }
        }
        public string Index { get; set; }

        #region  待阅相关的数据
        private void LoadPageData(int index)
        {
            int pageSize = 10;
            int count=0;
            IList list = read.GetList(GetWhereCondition(),0,10, out count);//start- limit
            PagedDataSource page = new PagedDataSource();


            int totalCount = list.Count;
            //DataView dv = dt.DefaultView;
            page.DataSource = list;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rpMyRead.DataSource = page;
            rpMyRead.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();
        }
        /// <summary>
        /// 获取条件
        /// </summary>
        /// <returns></returns>
        private string GetWhereCondition()
        {
            string returnString=String.Empty;
            returnString += "username!admin;processLabel!" + processName.Value + ";bussinessNum!" + bussinessID.Value + ";taskuser!" + applicantUser.Value + ";";
            if (ddlProcessCatalog.SelectedValue != "0")
            {
                returnString += "processname!" + ddlProcessCatalog.SelectedItem.Text + ";";
            }
            else
            {
                returnString += "processname!;";
            }
            if (!String.IsNullOrEmpty(startDate.Value) && !String.IsNullOrEmpty(endDate.Value))
            {
                returnString += "timesql!and (ir.start_time between to_date('" + startDate.Value + "','yyyy-MM-dd') and to_date('" + endDate.Value + "','yyyy-MM-dd'));";
            }
            
            return returnString;
        }
        #endregion

        #region 已阅相关数据
        private void LoadFinPageData(int index)
        {
            int pageSize = 10;
            int count = 0;
            IList list = read.GetListFin(GetFinWhereCondition(), 0, 10, out count);//start- limit
            PagedDataSource page = new PagedDataSource();


            int totalCount = list.Count;
            //DataView dv = dt.DefaultView;
            page.DataSource = list;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rpCompleteRead.DataSource = page;
            rpCompleteRead.DataBind();

            pagination2.RecordCount = totalCount;
            pagination2.PageSize = page.PageSize;
            pagination2.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination2.Visible = true;
            pagination2.AlwaysShow = true;
            pagination2.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination2.DataBind();
        }
        private string GetFinWhereCondition()
        {
            string returnString = String.Empty;
            returnString += "username!admin;processLabel!" + processNameCom.Value + ";bussinessNum!" + bussinessIDCom.Value + ";taskuser!" + applicantUserCom.Value + ";";
            if (ddlProcessNameFin.SelectedValue != "0")
            {
                returnString += "processname!" + ddlProcessNameFin.SelectedItem.Text + ";";
            }
            else
            {
                returnString += "processname!;";
            }
            if (!String.IsNullOrEmpty(startDateCom.Value) && !String.IsNullOrEmpty(endDateCom.Value))
            {
                returnString += "timesql!and (ir.start_time between to_date('" + startDateCom.Value + "','yyyy-MM-dd') and to_date('" + endDateCom.Value + "','yyyy-MM-dd'));";
            }

            return returnString;
        }
        #endregion
        /// <summary>
        /// 分页选择
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void pagination_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            LoadPageData(e.NewPageIndex - 1);
        }

        /// <summary>
        /// 分页选择
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void pagination2_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            LoadFinPageData(e.NewPageIndex - 1);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadPageData(0);
            Index = "0";
        }

        protected void btnSearchCom_Click(object sender, EventArgs e)
        {
            LoadFinPageData(0);
            Index = "1";
        }
        /// <summary>
        /// 撤销已阅
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rpCompleteRead.Items)
            {
                HtmlInputCheckBox checkbox = (HtmlInputCheckBox)item.FindControl("ckFinGroup");
                Hashtable hs;
                if (checkbox.Checked == true)
                {
                    string inboxID = checkbox.Value;
                    hs = new Hashtable();
                    hs.Add("status", "0");
                    hs.Add("inboxid", inboxID);
                    read.Update(hs);
                }
            }
            LoadPageData(0);
            LoadFinPageData(0);
            Index = "1";
        }
    }
}